﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.Aspect.Services.Mail;
using Microsoft.Practices.Unity;
using System.Windows;
using System.Net.Mail;
using System.Net.Mime;
using Infrastructure.WpfClient.Interceptors;

namespace Infrastructure.WpfClient.Views.ExceptionBox
{
    public class ExceptionBoxPresenter
    {
        public IExceptionBoxView View { get; set; }
        public ExceptionBoxPresenter()
        {        
        }
        public string CopyTo { get; set; }
        public string EmailMessage { get; set; }
        public string ReplyTo { get; set; }

        public void CopyToClipboard()
        {
            var ex = this.View.CurrentException;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(ex.Message);
            sb.AppendLine(ex.StackTrace);
            Clipboard.SetText(sb.ToString());
        }
        [BlockAsyncExecution]
        public virtual  void SendEmail()
        {
            try
            {

                var mail = Infrastructure.Aspect.Patterns.Singleton<UnityContainer, IUnityContainer>.Instance.Resolve<MailMessage>("SupportMessage");                                
                if (!string.IsNullOrEmpty(this.CopyTo)) 
                    mail.CC.Add(this.CopyTo);
                System.IO.MemoryStream mem = new System.IO.MemoryStream(this.View.CurrentImage);
                System.Net.Mail.Attachment att = new Attachment(mem, MediaTypeNames.Image.Jpeg);
                att.ContentId = "Screen.jpeg";
                att.Name = "Screen.jpeg";
                mail.Attachments.Add(att);                
                StringBuilder builder = new StringBuilder();
                builder.AppendLine("<html>");
                builder.AppendLine("<body>");
                builder.AppendLine("<p><strong>");
                builder.AppendLine(this.EmailMessage);
                builder.AppendLine("</strong> </p> ");
                builder.AppendLine("<p>");
                builder.AppendLine("<p><strong> Answer : ");
                builder.AppendLine(this.ReplyTo);
                builder.AppendLine("</strong> </p> ");
                builder.AppendLine("<p>");
                builder.AppendLine(this.View.CurrentException.Message);
                builder.AppendLine("</p>");
                builder.AppendLine("<p>");
                builder.AppendLine(this.View.CurrentException.StackTrace);
                builder.AppendLine("</p>");
                builder.AppendLine("<p>");
                builder.AppendLine("<img src='cid:Screen.jpeg'/>");
                builder.AppendLine("</p>");
                builder.AppendLine("</body>");
                builder.AppendLine("</html>");
                mail.Body = builder.ToString();
                System.Net.Mail.SmtpClient cl = Infrastructure.Aspect.Patterns.Singleton<UnityContainer,IUnityContainer>.Instance.Resolve<SmtpClient>("SupportMailClient");
                cl.Send(mail);                                               
            }
            catch (Exception ex)
            {
                
            }
        }
    }
}
